<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_Config_Xml
{
	private $_confgs = null;
	
	public static function getConfigFile()
	{
		$host = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
		$host = (substr($host, 0, 3) == 'www') ? substr($host, 4) : $host;
		$filename = CONFIG_DIR . DS. $host . '.xml';
		return $filename;
	}
	
	public static function getConfig($section = null)
	{
		$host = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
		$host = (substr($host, 0, 3) == 'www') ? substr($host, 4) : $host;
		$filename = CONFIG_DIR . DS. $host . '.xml';
		$default = CONFIG_DIR . DS. 'application.xml';
		$config = null;
		
		if (!file_exists($filename) && file_exists($default)) {
			$filename = $default;
		}
		
		if (!file_exists($filename)) {
			throw new Exception('Config File does not exist');
		}
		$config = simplexml_load_file($filename);
		if ($section && isset($config->$section)) {
			return $config->$section;
		}
		return $config;
	}
}